* Load the data file -----------------------------------------------------------

	if "$individual_run" != "Yes" {
	di "Program Stopped: Make sure you load the parameters in master.do"
	e
	}

	use $interim_data, clear
	
* Sample restriction and final data cleaning -----------------------------------

	* Limit the months to 1-12
	keep if inrange(month, 1, 12)
	
	* Rename variables to make them shorter
	* Since Stata has a restriction on matrix name
	
	gl oldnames l12_trr_C trr_nf_p2020_1_C trr_nf_p2020_2_C ///
				arrest_con_cop ///
				days_iod ///
				l23_trr_C trr_C ///
				l12_trr_s_i_C l12_trr_sa_injury_C l12_trr_s_h_C l12_trr_s_hos_i_ix_C ///
				ttt_score_cut_Am ///
				award total_w_exo_accusations ///
				prs_score_w_arrs_cut_Am ///
				i_complaint_pri_cut_Am i_complaint_sec_cut_Am i_complaints_cut_Am
				
	gl newnames NLU UL1 UL2 ///
				DAR ///
				OI ///
				LTU AU ///
				NLUSI NLUSA NLUH NLUSH ///
				TTT4 ///
				AW COM ///
				AI ///
				COMP COMS COMI
	
	* Rename
	rename ($oldnames) ($newnames)

		
	* Set of consideration for LASSO
	gl consideration_v	///
				pre_arrest_con_cop pre_prs_score_w_arrs pre_days_iod ///
				experience dblack dhispanic dwhite dmale
				
				
	* Set of hand-picked variales for non-LASSO regression
	gl picked5a		///
				pre_arrest_con_cop pre_prs_score_w_arrs pre_days_iod ///
				experience dblack dwhite dhispanic dmale 
				
	
* Shared parameters for regressions --------------------------------------------
		
	* Variable names
	
			* Names of all FBI variable - this will be used in the loop
			gl fbi_name
			gl fbi_nd_name
			gl fbi_d_name
			foreach o in 	01A 01B 02 03 04A 04B 05 06 07 08A 08B 09 ///
							10 11 12 13 14 15 16 17 18 19 ///
							20 21 22 24 26 36 MCC NA TRF WRT others {
							gl fbi_name 		$fbi_name 		arr_FBI_all_`o'
							gl fbi_nd_name		$fbi_nd_name 	arr_FBI_ND_`o'
							gl fbi_d_name		$fbi_d_name 	arr_FBI_D_`o'
			}
	
			* Names of all PRS variables
			gl prs ///
			prs_firearm prs_veh_rec prs_warrant prs_traf_stop prs_driv_stop ///
			prs_cont prs_anov prs_haz_cit prs_nonhaz_cit ///
			prs_curf_vio prs_cta prs_park_cit prs_isrs
			
	* New outcome
	
		* Other non-index arrest
		gen arr_FBI_all_other = arr_ni - arr_FBI_all_MCC
		
		* Race specific arrest 
		
		foreach i in $prs arrest_non_con_cop AI arr_FBI_all_other {
				foreach v in all black nblack white hisp other {
				gen `i'_`v' = `i'
		}
		}
		
		* Race specific index
		foreach o in a d nd {
		foreach r in all black nblack white hisp other {
		qui: sum arr_`o'_`r' if (months_1_4==1 & treatment ==0)
		qui: gen z_`o'_`r' = (arr_`o'_`r' - r(mean)) / r(sd) if (months_1_4==1)
		}
		}
		
	

* (1A) OLS regressions: All Arrest-Race Outcomes -------------------------------

	* Loop through race
	foreach race in ///
	all black nblack white hisp other {
	
		* Loop through arrest types
		foreach type in ///
				arr_a ///
				arr_i arr_ni ///
				arr_d arr_nd ///
				z_a z_d z_nd ///
				arr_v arr_p arr_F arr_M  ///
				$fbi_name $fbi_nd_name $fbi_d_name ///
				$prs arrest_non_con_cop AI ///
				arr_FBI_all_other { // note that this other is not FBI "others"
				
			* Loop through periods
			foreach period in 1_4 {	
	
				* Define outcome
				local outcome `type'_`race'
				
					* To stop the loop from breaking for this sub-race (nonuse)
					if "`outcome'" == "z_d_other" {
					local outcome z_d_all
					}
		
				* CM and ITT
				qui: sum `outcome' if (treatment == 0) & (months_`period' == 1)
				local CM = r(mean)
				
				* Canonical regression
				qui: reg `outcome' treatment ${picked5a} i.strata i.year_month ///
				if (months_`period' == 1), vce(cluster employee_id)
				qui: test treatment
			
				* Form the matrix
				mat add = `CM', _b[treatment], _se[treatment], r(p),e(N)
				mat rownames add = `outcome'_`period'
				mat `type'_`race'_m_`period' = add
				*mat li `type'_`race'_m_`period'
			
	}
	}
	}
	

	
	
	
	
* (1B) Write Latex -------------------------------------------------------------

	* Table 4: Discretionary Arrests - without FDR adjustment	
	gl month_segments m_1_4 
	table_start_horizontal ${report_folder}/Admin_arr_discretionary.txt ///
	"Arrests of Black Subjects and Other Subjects" 220 "one_p_no_q"
	midrule

		write_horizontal 	arr_d_black		1000		"Discretionary arrests: Black subjects" 	CmCoSePvRq
		write_horizontal 	arr_d_nblack	1000		"Discretionary arrests: Other subjects" 	CmCoSePvRq
	
		write_horizontal 	arr_a_black		1000		"All arrests: Black subjects" 				CmCoSePvRq
		write_horizontal 	arr_a_nblack	1000		"All arrests: Other subjects" 				CmCoSePvRq

		write_horizontal 	arr_nd_black	1000		"Other arrests: Black subjects" 			CmCoSePvRq
		write_horizontal 	arr_nd_nblack	1000		"Other arrests: Other subjects" 			CmCoSePvRq

	local N : di %9.0fc arr_a_black_m_1_4[1,5]
	write_end end_arr_1 `N'
	table_end "$end_arr_1" Admin_arr_discretionary
	
	* Table B24: * Z-score on Discretionary Arrests - without FDR adjustment	
	gl month_segments m_1_4 
	table_start_horizontal ${report_folder}/Admin_arr_discretionary_z.txt ///
	"Arrests of Black Subjects and Other Subjects (Z-Scores)" 220 "one_p_no_q"
	midrule

		write_horizontal 	z_d_black	1		"Discretionary arrests: Black subjects" 	RcCoSePvRq
		write_horizontal 	z_d_nblack	1		"Discretionary arrests: Other subjects" 	RcCoSePvRq
	
		write_horizontal 	z_a_black	1		"All arrests: Black subjects" 				RcCoSePvRq
		write_horizontal 	z_a_nblack	1		"All arrests: Other subjects" 				RcCoSePvRq

		write_horizontal 	z_nd_black	1		"Other arrests: Black subjects" 			RcCoSePvRq
		write_horizontal 	z_nd_nblack	1		"Other arrests: Other subjects" 			RcCoSePvRq

	local N : di %9.0fc z_a_black_m_1_4[1,5]
	write_end end_arr_1_z `N'
	table_end "$end_arr_1_z" Admin_arr_discretionary_z
	

	* Table B15: Non-Index Arrests
	gl month_segments m_1_4 
	table_start_horizontal ${report_folder}/Admin_arr_NI.txt ///
	"Effects on Arrests for Non-index Crimes" 220 "one_p_no_q"
	midrule

		write_horizontal 	arr_ni_all				1000		"Non-index arrests" 			CmCoSePvRq
		midrule
		write_horizontal 	arr_FBI_all_MCC_all		1000		"Chicago municipal code violations" 			CmCoSePvRq
		write_horizontal 	arr_FBI_all_17_all		1000		"Criminal sexual abuse" 			CmCoSePvRq
		write_horizontal 	arr_FBI_all_21_all		1000		"Driving under the influence" 	CmCoSePvRq	
		write_horizontal 	arr_FBI_all_18_all		1000		"Drug abuse" 					CmCoSePvRq
		write_horizontal 	arr_FBI_all_19_all		1000		"Gambling"		 				CmCoSePvRq
		write_horizontal 	arr_FBI_all_22_all		1000		"Liquor license" 				CmCoSePvRq
		write_horizontal 	arr_FBI_all_24_all		1000		"Mob action, loitering and disorderly offenses" CmCoSePvRq	
		write_horizontal 	arr_FBI_all_20_all		1000		"Offenses against family"  		CmCoSePvRq
		write_horizontal 	arr_FBI_all_16_all		1000		"Prostitution"	 				CmCoSePvRq
		write_horizontal 	arr_FBI_all_TRF_all		1000		"Traffic offenses" 					CmCoSePvRq
		write_horizontal 	arr_FBI_all_WRT_all		1000		"Warrant" 							CmCoSePvRq
		write_horizontal 	arr_FBI_all_15_all		1000		"Weapon violations" 			CmCoSePvRq
		write_horizontal 	arr_FBI_all_26_all		1000		"Miscellaneous non-index offenses" 	CmCoSePvRq		
		

	local N : di %9.0fc arr_FBI_all_15_all_m_1_4[1,5]
	write_end end_arr_NI `N'
	table_end "$end_arr_NI" Admin_arr_NI


	* Table B23: Discretionary arrest - all sub-race
	gl month_segments m_1_4 
	table_start_horizontal ${report_folder}/Admin_arr_discretionary_race.txt ///
	"Arrests by Race of Subject" 220 "one_p_no_q"
	midrule

		write_horizontal 	arr_d_black		1000		"Discretionary arrests: Black subjects" 		CmCoSePvRq
		write_horizontal 	arr_d_white		1000		"Discretionary arrests: White subjects" 		CmCoSePvRq
		write_horizontal 	arr_d_hisp		1000		"Discretionary arrests: Hispanic subjects" 		CmCoSePvRq
		
		midrule
		
		write_horizontal 	arr_a_black		1000		"All arrests: Black subjects" 					CmCoSePvRq
		write_horizontal 	arr_a_white		1000		"All arrests: White subjects" 					CmCoSePvRq
		write_horizontal 	arr_a_hisp		1000		"All arrests: Hispanic subjects" 				CmCoSePvRq
		write_horizontal 	arr_a_other		1000		"All arrests: All other race subjects" 			CmCoSePvRq
		midrule

		write_horizontal 	arr_nd_black	1000		"Other arrests: Black subjects" 				CmCoSePvRq
		write_horizontal 	arr_nd_white	1000		"Other arrests: White subjects" 				CmCoSePvRq
		write_horizontal 	arr_nd_hisp		1000		"Other arrests: Hispanic subjects" 				CmCoSePvRq
		write_horizontal 	arr_nd_other	1000		"Other arrests: All other race subjects" 		CmCoSePvRq

	local N : di %9.0fc arr_a_black_m_1_4[1,5]
	write_end end_arr_2 `N'
	table_end "$end_arr_2" Admin_arr_discretionary_race
		
	
	* Table B25: Component of activity index
	gl month_segments m_1_4 
	table_start_horizontal ${report_folder}/Admin_App_OI.txt ///
	"Components of the Officer Activities Index" 220 "one_p_no_q"
	midrule
		write_horizontal 	AI_all  				1	"Officer activities (index)" 	RcCoSePvRq
		midrule
	
		write_horizontal 	prs_anov_all  			1000	"ANOVs" 				CmCoSePvRq
		write_horizontal 	prs_haz_cit_all  		1000	"Citations - Hazard" 	CmCoSePvRq
		write_horizontal 	prs_cta_all  			1000	"CTA checks" 			CmCoSePvRq
		write_horizontal 	prs_curf_vio_all  		1000	"Curfew violations" 	CmCoSePvRq
		write_horizontal 	prs_driv_stop_all  		1000	"Driver stops" 			CmCoSePvRq
		write_horizontal 	prs_isrs_all  			1000	"ISRs" 					CmCoSePvRq		
		write_horizontal 	arrest_non_con_cop_all  1000	"Other arrests" 		CmCoSePvRq 
		write_horizontal 	prs_park_cit_all  		1000	"Parking citations" 	CmCoSePvRq
		write_horizontal 	prs_firearm_all  		1000	"Recovered guns" 		CmCoSePvRq	
		write_horizontal 	prs_veh_rec_all  		1000	"Recovered vehicles" 	CmCoSePvRq	
		write_horizontal 	prs_traf_stop_all  		1000	"Traffic stops" 		CmCoSePvRq
		write_horizontal 	prs_warrant_all  		1000	"Warrants" 				CmCoSePvRq

		
	local N : di %9.0fc arr_nd_all_m_1_4[1,5]
	write_end end_arr_OI `N'
	table_end "$end_arr_OI" Admin_App_OI